
package com.insigma.business.qrcode.soldier.service.impl;

import cn.hutool.core.date.DateUtil;

import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.soldier.dao.SoldierDailyLoginDao;
import com.insigma.business.qrcode.soldier.dto.SequenceNameConstant;
import com.insigma.business.qrcode.soldier.dto.SoldierDailyLoginBSearcher;
import com.insigma.business.qrcode.soldier.entity.SoldierDailyLoginDO;
import com.insigma.business.qrcode.soldier.service.SoldierDailyLoginService;
import com.insigma.business.qrcode.soldier.util.RedisLock;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service("soldierDailyLoginService")
public class SoldierDailyLoginServiceImpl implements SoldierDailyLoginService {

    @Autowired(required = false)
    private SoldierDailyLoginDao soldierDailyLoginDao;
    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    @Autowired
    private RedisLock redisLock;

    private String lockPrefix = "daily:login:";

    

    public List<SoldierDailyLoginDO> search(final SoldierDailyLoginBSearcher soldierDailyLoginBSearcher) {
        return soldierDailyLoginDao.search(soldierDailyLoginBSearcher);
    }

    
    @Override
    public SoldierDailyLoginDO get(final String id) {
        return soldierDailyLoginDao.get(id);
    }

    
    @Override
    public Integer insert(final SoldierDailyLoginDO soldierDailyLoginDO) {
        soldierDailyLoginDO.setId(sequenceGenerator.genKey(SequenceNameConstant.SOLDIER_DAILY_LOGIN_ID));
        boolean b = redisLock.tryLock(lockPrefix + soldierDailyLoginDO.getSoldierId(), 500);
        if (b) {
            return soldierDailyLoginDao.insert(soldierDailyLoginDO);
        }
        return 0;
    }

    
    @Override
    public Integer update(final SoldierDailyLoginDO soldierDailyLoginDO) {
        return soldierDailyLoginDao.update(soldierDailyLoginDO);
    }

    
    @Override
    public Integer delete(final String id, final String modifierType, final String modifierId) {
        return soldierDailyLoginDao.delete(id, modifierType, modifierId);
    }

    @Override
    public SoldierDailyLoginDO addIfFirstAndGet(String soldierId, String realName, String idCardNo) {
        SoldierDailyLoginDO last = soldierDailyLoginDao.getLastBySoldierId(soldierId);
        if (last == null) {
            last = new SoldierDailyLoginDO();
            last.setIdCardNo(idCardNo);
            last.setRealName(realName);
            last.setSoldierId(soldierId);
            last.setIntegral(1);
            last.setLoginDays(1);
            insert(last);
            return last;
        } else {

            String todayStr = DateUtil.formatDate(new Date());
            String loginStr = DateUtil.formatDate(last.getCreateDatetime());
            if (loginStr.equals(todayStr)) {
                return last;
            } else {
                
                SoldierDailyLoginDO today = new SoldierDailyLoginDO();
                today.setSoldierId(soldierId);
                today.setIdCardNo(idCardNo);
                today.setRealName(realName);


                if (loginStr.equals(DateUtil.formatDate(DateUtils.addDays(new Date(), -1)))) {
                    
                    today.setLoginDays(last.getLoginDays() + 1);
                    today.setIntegral(last.getIntegral() + getScoreByLoginDays(today.getLoginDays()));
                } else {
                    
                    today.setLoginDays(1);
                    today.setIntegral(last.getIntegral() + 1);
                }
                insert(today);
                return today;
            }
        }
    }

    private Integer getScoreByLoginDays(Integer loginDays) {
        if (loginDays < 7) {
            return loginDays;
        }

        return 7;
    }

}
